import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_k9/esc_command.dart';
import 'package:flutter_k9/flutter_k9.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _flutterK9Plugin = FlutterK9();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion = await _flutterK9Plugin.getPlatformVersion() ??
          'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  void _connect() async {
    final result = await _flutterK9Plugin.connect();
    print(result);
  }

  void _print() async {
    _flutterK9Plugin.print([
      TextCommand(
        "LOVE GIFT",
        textSize: 26,
        scaleWidth: 2,
        scaleHeight: 2,
        itemAlign: PrintItemAlign.CENTER,
      ),
      TextCommand(
        "爱礼",
        textSize: 24,
        scaleWidth: 2,
        scaleHeight: 2,
        itemAlign: PrintItemAlign.CENTER,
      ),
      TextCommand(
        "订单",
        textSize: 24,
        scaleWidth: 2,
        scaleHeight: 2,
        itemAlign: PrintItemAlign.CENTER,
      ),
      TextCommand(
        "兰州中天健官场店",
        textSize: 24,
        scaleWidth: 2,
        scaleHeight: 2,
        itemAlign: PrintItemAlign.CENTER,
      ),
      TextCommand(
        "自提单",
        textSize: 24,
        scaleWidth: 2,
        scaleHeight: 2,
        itemAlign: PrintItemAlign.CENTER,
      ),
      TextCommand(
        "收货人",
        textSize: 24,
      ),
      TextCommand(
        "刘",
        textSize: 24,
        scaleWidth: 2,
        scaleHeight: 2,
      ),
      TextCommand(
        "13291118829",
        textSize: 24,
        scaleWidth: 2,
        scaleHeight: 2,
      ),
      TextCommand(
        "自提/配送时间：2023-08-30 23:22:46",
        textSize: 24,
      ),
      TextCommand(
        "-----------------------------------------------",
      ),
      TextCommand(
        "订单号：2023-08-30 23:22:46",
        textSize: 24,
      ),
      TextCommand(
        "下单时间：2023-08-30 23:22:46",
        textSize: 24,
      ),
      TextCommand(
        "-----------------------------------------------",
      ),
      MultipleTextCommand(
        [2.2, 1.3, 1, 1],
        [
          TextCommand("名称"),
          TextCommand("单价", itemAlign: PrintItemAlign.CENTER),
          TextCommand("数量", itemAlign: PrintItemAlign.CENTER),
          TextCommand("金额", itemAlign: PrintItemAlign.RIGHT),
        ],
      ),
      TextCommand(
        "-----------------------------------------------",
      ),
      MultipleTextCommand(
        [2.2, 1.3, 1, 1],
        [
          TextCommand("慢倾心严#6"),
          TextCommand("188.0", itemAlign: PrintItemAlign.CENTER),
          TextCommand("1", itemAlign: PrintItemAlign.CENTER),
          TextCommand("169.2", itemAlign: PrintItemAlign.RIGHT),
        ],
      ),
      TextCommand(
        "-----------------------------------------------",
      ),
      TextCommand(
        "总价：169.2",
        textSize: 24,
      ),
      TextCommand(
        "配送费：5.0",
        textSize: 24,
      ),
      TextCommand(
        "实付金额：174.2",
        textSize: 24,
        lineSpace: 2
      ),
    ]);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              ElevatedButton(
                onPressed: () {
                  _connect();
                },
                child: const Text("连接"),
              ),
              ElevatedButton(
                onPressed: () {
                  _print();
                },
                child: const Text("打印"),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
